<template>
	<view class="homeLayout">
		<view class="banner">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="(item,index) in swiperList" :key="index">
					<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=`+item.goods_id">
						<image :src="item.image_src" mode="aspectFill"></image>
					</navigator>
				</swiper-item>
			</swiper>
		</view>
		<view class="nav-list">
			<view class="nav-item" v-for="(item,index) in navList" :key="index" @click="navClickHandler(item)">
				<image class="nav-img" :src="item.image_src" mode="aspectFill"></image>
			</view>
		</view>
		<view class="floor-list">
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<!-- 楼层的标题 -->
				<image :src="item.floor_title.image_src" mode="aspectFill" class="floor-title"></image>
				<!-- 楼层的图片区域 -->
				<view class="floor-img-box">
					<!-- 左侧的大图片 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						<image :src="item.product_list[0].image_src"
							:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix">
						</image>
					</navigator>
					<view class="right-img-box">
						<!-- 右侧的4张小图片 -->
						<navigator class="right-img-item" v-for="(item2,index2) in item.product_list.slice(1,5)" :key="index2" :url="item2.url">
							<image :src="item2.image_src"  :style="{width: item2.image_width + 'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
					
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,watch
	} from "vue";
	import {
		onLoad,onShow
	} from '@dcloudio/uni-app'
	import { cartMixin } from '@/mixins/tabbar-badge.js';
	const {cartStore,setBadge } = cartMixin.setup();
	
	const swiperList = ref([])
	const navList = ref([])
	const floorList = ref([])
	onLoad(() => {
		getSwiperList()
		getNavList()
		getFloorList()
		// console.log(setBadge)
	})
	watch(cartStore.total,()=>{
		setBadge()
	})
	
	//获取轮播图数据的方法
	const getSwiperList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/swiperdata')
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		swiperList.value = res.message

	}
	//获取分类导航的数据的方法
	const getNavList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/catitems')
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		navList.value = res.message
	}
	//定义获取楼层列表数据的方法
	const getFloorList = async () => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/home/floordata')
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		res.message.forEach(floor=>{
			floor.product_list.forEach(item=>{
				item.url='/subpkg/goods_list/goods_list?'+item.navigator_url.split('?')[1]
			})
			
		})
		floorList.value = res.message
		// console.log(floorList.value)
	}

	//点击导航
	const navClickHandler = (item) => {
		if (item.name === '分类') {
			uni.switchTab({
				url: "/pages/cate/cate"
			})
		}
	}
</script>

<style lang="scss">
	.homeLayout {
		padding: 0;
		margin: 0;

		.banner {
			width: 750rpx;

			swiper {
				width: 750rpx;
				height: 330rpx;

				.swiper-item {
					height: 100%;
					width: 100%;

					image {
						height: 100%;
						width: 100%;
					}
				}
			}

		}

		.nav-list {
			display: flex;
			justify-content: space-around;
			margin: 15px 0;

			.nav-img {
				width: 128rpx;
				height: 140rpx;
			}
		}

		.floor-list {
			.floor-title {
				height: 60rpx;
				width: 100%;
			}

			.floor-img-box {
				display: flex;
				padding-left: 15rpx;
				.right-img-box{
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
				
			}
		}

	}
</style>